<?php
class Admin_Model_Menu extends Zend_Db_Table_Abstract
{
	/**
	 * Model of menu modules
	 * @author Nguyen Manh Hung
	 */
	
	protected $_name = TABLE_MENU;
	
	/**
	 * @author Nguyen manh Hung
	 * @param $name
	 * @param $description
	 * @param $keyword
	 * @param $type
	 * @param $parent
	 * @param $order
	 * @param $status
	 * @param $games
	 */
	public function insertMenu($name, $description="", $keyword="", $type = "Games", $parent ="", $order = 1, $status =1, $games=0)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$row = $this->createRow();
		$row->name = $name;
		$row->parent = $parent;
		$row->desc = $description;
		$row->type = $type;
		$row->keywords = $keyword;
		$row->order = $order;
		$row->active = $status;
		$row->games = $games;
		$row->save();
		$id =$row->id;
		return $id;
	}
	
	/**
	 * update menu
	 * @author Nguyen Manh Hung
	 * @param int $id
	 * @param String $name
	 * @param Int $parent
	 * @param Int $order
	 * @param Int $status
	 */
	public function updateMenu($id, $name, $description="", $keyword="", $type = "Games", $parent ="", $order = 1, $status =1)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$row = $this->find($id)->current();
		if($row){
			$row->name = $name;
			$row->parent = $parent;
			$row->desc = $description;
			$row->type = $type;
			$row->keywords = $keyword;
			$row->order = $order;
			$row->active = $status;
			$row->save();
			return 1;
		}else{
			return 0;
		}
	}
	
	/**
	 * @author Nguyen manh HUng
	 * @param int $id
	 * @param int $order
	 * @return number|number
	 */
	public function updateOrder($id, $order=0){
		$db = Zend_Db_Table::getDefaultAdapter();
		$row = $this->find($id)->current();
		if($row){
			$row->order = $order;
			$row->save();
			return 1;
		}else{
			return 0;
		}
	}
	
	/**
	 * @author Nguyen manh HUng
	 * @param int $id
	 * @param int $home
	 * @return number|number
	 */
	public function updateHome($id, $home=1){
		if ($home>0) {
			$home = 1;
		}else{
			$home = 0;
		}
		$db = Zend_Db_Table::getDefaultAdapter();
		$row = $this->find($id)->current();
		if($row){
			$row->home = $home;
			$row->save();
			return 1;
		}else{
			return 0;
		}
	}
	
	/**
	 * delete Menu 
	 * @author Nguyen Manh HUng
	 * @param Int $id
	 */
	public function deleteMenu($id){
		$db = Zend_Db_Table::getDefaultAdapter();
		$id = $db->quote($id, INTEGER);
		$rowMenu = $this->find($id)->current();
		if($rowMenu){
			$rowMenu->delete();
			return 1;
		}else{
			return 0;
		}
	}
	
	/**
	 * fetch all menu
	 * @author Nguyen Manh Hung (manhhung86it@gmail.com)
	 * @param Array $filters
	 * @param String $sortFeild
	 * @param Int $limit
	 * @param Int $page
	 */
	public function fetchMenu($filters = array(), $sortFeild = null, $limit = null, $page = 1)
	{
		$select = $this->select();
		//add cac filter vao truy van tim kiem
		if(count($filters)>0)
		{
			foreach ($filters as $feild => $filter){
				$select->where($feild .' =?', $filter);
			}
		}
		//add sort vao truy van
		if(null != $sortFeild)
		{
			$select->order($sortFeild);
		}
		//add limit - offset vao truy van		
		if(null != $limit)
		{
			$offset = $page * NUM_PER_PAGE;
			if($offset<=$limit)
			{
				$offset = $limit+NUM_PER_PAGE;
			}
			$select->limit($limit, $offset);
		}		
		return $this->fetchAll($select);
	}
	
	public function itemInSelectbox($arrParam = null){
		
		$select = $this->select();
		if(count($arrParam)>0)
		{
			foreach ($arrParam as $feild => $filter){
				$select->where($feild .' =?', $filter);
			}
		}
		$select->order('order');
		$result  = $this->fetchAll($select);

		$result = $result->toArray();
		$system = new Hung_System_Recursive($result);
		$result = $system->buildArray(0);
		//$tmp = array('id'=>0,'name'=>'Root category','level'=>1,'order'=>1,'parent'=>'');
		//array_unshift($result,$tmp);
		
		return $result;
	} 
}